package com.hgx.servlet.demo.servlet;

/**
 * Created by liujun on 2018/5/27.
 */

import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author hugx
 * @create 2018-05-27 17:19
 */
public class HelloWorldServlet extends HttpServlet {

    private static final Logger logger=Logger.getLogger(HelloWorldServlet.class);
    /**
     * 查看httpservlet实现的service一看便知，起到了一个controll控制器的作用(转向的)
     * 之后便是跳转至我们熟悉的doget,dopost等方法中
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("doservice..."+this.getInitParameter("encoding"));
        String userName=req.getParameter("userName");
        System.out.println("userName: "+userName);

        ServletContext context=req.getServletContext();
        String conName=context.getServletContextName();

        super.service(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        logger.info("[doGet] Test before userName: "+getInitParameter("userName")+"  , age:  "+getInitParameter("age")+" ,  Thread_id: "+Thread.currentThread().getId()+"  , "+Thread.currentThread().getName());
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        logger.info("[doGet] Test after , *****  Thread_id: "+Thread.currentThread().getId()+"  , "+Thread.currentThread().getName());
//        doPost(req, resp);

        resp.setContentType("text/plain");
        resp.getWriter().write("OK");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("dopost...");
    }

}
